package com.woniu.utils;

import eu.bitwalker.useragentutils.UserAgent;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author pml
 * @Date 2025/6/18 23:09
 * @Description TODO
 * User-Agent解析工具类
 * 提取设备信息和浏览器信息
 */
public class UserAgentUtils {
    public static Map<String, String> parseUserAgent(String userAgentString) {
        Map<String, String> result = new HashMap<>();
        if (userAgentString == null || userAgentString.isEmpty()) {
            result.put("device", "未知设备");
            result.put("browser", "未知浏览器");
            return result;
        }
        UserAgent userAgent = UserAgent.parseUserAgentString(userAgentString);
        // 获取浏览器及版本
        String browser = userAgent.getBrowser().getName() + " " + userAgent.getBrowserVersion();
        // 获取操作系统（设备信息）
        String device = userAgent.getOperatingSystem().getName();
        result.put("device", device);
        result.put("browser", browser);
        return result;
    }
}
